# LuckPay Web Manage System
#
# Copyright (c) 2016 Lucky Byte, Inc.
#
$ ->
    # 表单字段验证
    form = $('form.main')
    form.form
        inline: true,
        fields:
            t20_termid:     optional: false, rules: [ type: 'empty' ]
            tz0_termid:     optional: false, rules: [ type: 'empty' ]
            t20_fee:        optional: false, rules: [ type: 'empty' ]
            pay_chnl:       optional: false, rules: [ type: 'empty' ]
            pay_merid:      optional: false, rules: [ type: 'empty' ]
            acct_no:        optional: false, rules: [
                { type: 'empty' }, { type: 'integer[..]' },
            ]
            acct_name:      optional: true,  rules: [ type: 'doesntContain[ ]' ]
            acct_mobile:    optional: true,  rules: [ type: 'integer[..]' ]

    # 页面差异突出显示
    diffs = form.form 'get value', 'diffs'
    if diffs and diffs.trim().length > 0
        diffs = JSON.parse diffs
        form.form 'set value', 'diffs', ''
        for name in diffs
            field = form.find("input[name=#{name}]").parents('.field')
            field.addClass('changed')

    # 页面数据集
    record = form.form 'get value', 'record'
    if record and record.trim().length > 0
        record = JSON.parse record
        form.form 'set value', 'record', ''
    else
        record = null

    # 改变代付渠道时，更新代付商户列表
    form.find('.dropdown.pay_chnl').dropdown
        onChange: (value, text, $choice) ->
            $(this).addClass 'loading'
            $.post '/p1/pay_merids', {
                chnl: value
            }, (resp) ->
                form.find('.dropdown.pay_chnl').removeClass 'loading'
                return if not resp.succ
                form.find('.dropdown.pay_merid').dropdown 'clear'
                items = ''
                for merid in resp.merids
                    items += "<div class='item' data-value='#{merid.uuid}'>"
                    items += "#{merid.merno}"
                    items += "<div class='right floated'
                        style='font-size:small; color:#888;'>#{merid.name}"
                    items += "</div>"
                    items += "</div>"
                form.find('.dropdown.pay_merid .menu').html items
                form.find('.dropdown.pay_merid').dropdown 'refresh'

                # 恢复代付商户的值(编辑时)
                if record and record.pay_merid
                    form.form 'set value', 'pay_merid', record.pay_merid

    # 恢复界面的值，如果是复核，则冻结界面元素
    if record
        form.form 'set values', record
        if record._checking
            acct_no = form.form 'get value', 'acct_no'
            if acct_no.length > 0
                form.find('.input.acct_no').removeClass('nofreeze')
            $('body').css('background-color', 'black')
            freeze_form(form)

    # 提交前处理
    form.submit (evt) ->
        if not $(this).form('is valid')
            return evt.preventDefault()

        # 如果开户人证件类型为身份证且身份证号码不为空，则需要验证身份证号码有效性
        cred_type = form.form 'get value', 'acct_cred_type'
        cred_no   = form.form 'get value', 'acct_cred_no'
        if parseInt(cred_type) == 1 and cred_no.trim().length > 0
            result = validate_idno(cred_no, '收款账户开户人')
            if not result.succ
                alert result.errmsg
                form.find('button#ok').removeClass 'loading'
                return evt.preventDefault()

        # 验证卡号，不一定完全准确，因此只能给用户提示
        acct_no = form.form 'get value', 'acct_no'
        if acct_no.length > 0
            if not validate_cardno acct_no
                if not confirm "收款账号可能存在错误，选择［取消］按钮返回检查，或［确定］按钮继续提交."
                    form.find('button#ok').removeClass 'loading'
                    return evt.preventDefault()

        # 账户姓名和证件号码必须提供一个
        acct_name = form.form 'get value', 'acct_name'
        if cred_no.trim().length == 0 and acct_name.trim().length == 0
            alert '账户开户人姓名和证件号码必须至少填写一个'
            form.find('button#ok').removeClass 'loading'
            return evt.preventDefault()
